$(document).ready(
		function() {
			$(".check").click(
					function(event) {
						var id = parseInt($(this).attr('id').match(/[0-9]+/g)
								.join(''), 10);
						var thisAmount = parseFloat($("#debt" + id).attr(
								"textContent"));
						var total = parseFloat($("#totalAccount").val());
						if (this.checked) {
							total = total + thisAmount;
						} else {
							total = total - thisAmount;
						}
						$("#totalAccount").val(total.toFixed(2));
					});

			$(function() {
				$("#dateCheck").datepicker({
					dateFormat : 'dd-mm-yy',
					changeMonth : true,
					changeYear : true,
					yearRange : "-1:+90"
				});
			});
			$('#amountCash').change(function() {
				updateAmounts();
			});

			$('#amountCreditCard').change(function() {
				updateAmounts();
			});

			$('#amountDebitCard').change(function() {
				updateAmounts();
			});

			$('#amountCheck').change(function() {
				updateAmounts();
			});

			updateAmounts = function() {
				var debit = parseFloat($('#amountDebitCard').val());
				var credit = parseFloat($('#amountCreditCard').val());
				var check = parseFloat($('#amountCheck').val());
				var cash = parseFloat($('#amountCash').val());
				var payed = debit + credit + check + cash;
				var currentAccount = parseFloat($('#totalAccount').val());
				var currentPayed = parseFloat($('#totalSale').val());
				$('#totalSale').val(payed.toFixed(2));
				$('#totalAccount').val(
						(currentAccount + currentPayed - payed).toFixed(2));
			}

			$('#addPaymentLink').click(function() {
				$("#paymentDiv").toggle();
			});
});
